package org.abettor.pushbox.activity;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Random;

import org.abettor.android.ads.YoumiManager;
import org.abettor.pushbox.config.Config;
import org.abettor.pushbox.db.PushBoxMapBeanManage;
import org.abettor.pushbox.map.Boxmap;
import org.abettor.pushbox.map.BoxmapUtil;
import org.abettor.pushbox.map.Step;
import org.abettor.pushbox.util.Resource;
import org.abettor.pushbox.R;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

public class ClassicShowResultActivity extends AbstractShowResultActivity{
	private PushBoxMapBeanManage manage;
	private int map_id = -1;
	private int mission_id = -1;
	private final int REPUSHMAP_ID = Menu.FIRST;
	private final int SHOW_AD = 13579;
	private YoumiManager youmi = new YoumiManager(this);
	private Handler youmiHandle = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (msg.what == SHOW_AD) {
				// 按照一定的概率显示广告
				Random rand = new Random(System.currentTimeMillis());
				int val = rand.nextInt(99);
				if (val < 100) {
					youmi.showYoumi(YoumiManager.Position.middle, Color.WHITE,
							Color.BLACK, 0xAA);
				}
			}

			super.handleMessage(msg);
		}
	};
	private Thread youmiDelayThread = new Thread() {
		@Override
		public void run() {
			try {
				Thread.sleep(200);
				Message msg = new Message();
				msg.what = SHOW_AD;
				youmiHandle.sendMessage(msg);
			} catch (InterruptedException e) {
				Log.d(this.getClass().getName(), "", e);
			}
		}
	};

	@Override
	protected Boxmap initMap() {
		byte[] buf = null;
		try {
			buf = readFile(map_id);
		} catch (IOException e) {
		}
		m_map = BoxmapUtil.paraArrayToMap(buf);// 生成地图
		return m_map;
	}
	/**
	 * 创建系统菜单
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		boolean result = super.onCreateOptionsMenu(menu);
		menu.add(0, REPUSHMAP_ID, 1, R.string.restart_game).setIcon(
				R.drawable.reset);
		return result;
	}
	/**
	 * 系统菜单执行的操作
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case REPUSHMAP_ID:// 重新开始游戏
			restart();
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	//重新开始游戏
	private void restart(){
		Intent in = new Intent();
		Bundle extras = new Bundle();
		extras.putInt("_id", map_id);
		in.putExtras(extras);
		in.setClass(ClassicShowResultActivity.this, ClassicPushBoxActivity.class);
		startActivity(in);
		ClassicShowResultActivity.this.finish();
	}
	// 读取地图文件
	private byte[] readFile(int mapId) throws IOException {
		int missionId = manage.getMissionId(mapId);
		InputStream bin = getResources().openRawResource(
				Resource.getMapResourceByMission(missionId));
		ByteArrayOutputStream bo = new ByteArrayOutputStream();
		int v = 0;
		while ((v = bin.read()) != -1) {
			bo.write(v);
		}
		byte[] buf = bo.toByteArray();
		bin.close();
		bo.close();
		return buf;
	}
	@Override
	protected List<Step> initStep() {
		String folder = Config.getClassicBasePath() + File.separator + String.valueOf(this.mission_id) + ".step";
		try {
			byte[] buf = readFileByPath(folder);
			return BoxmapUtil.paraArrayToStep(buf);
		} catch (IOException e) {
			return null;
		}
	}
	@Override
	public void onCreate(Bundle savedInstanceState){
		init();
		super.onCreate(savedInstanceState);
	}

	private void init() {
		Intent intent = getIntent();
		manage = new PushBoxMapBeanManage(this);
		Bundle extras = intent.getExtras();
		map_id = extras.getInt("_id");// 获得数据的id
		mission_id = manage.getMissionId(map_id);
		youmiDelayThread.start();
	}

}
